/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.utils;

import cn.ac.nercis.pes.community.model.vo.manage.CurrentUserVO;
import cn.ac.nercis.pes.community.model.vo.project.CurrentSystemVO;
import cn.ac.nercis.pes.community.support.AbstractFxmlView;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import lombok.NonNull;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * App全局数据类
 * @author zpy
 * @version 1.0.0
 */
public class AppStore {

    private static final SimpleObjectProperty<CurrentUserVO> CURRENT_USER = new SimpleObjectProperty<>();
    private static final List<CurrentSystemVO> CURRENT_SYSTEMS = new ArrayList<>();
    private static final Map<String, Class<? extends AbstractFxmlView>> MENU_VIEWS = new HashMap<>();
    private static final Map<String,Class<?>> MENU_CONTROLLERS = new HashMap<>();

    public static void setCurrentUser(CurrentUserVO user) {
        CURRENT_USER.set(user);
    }

    public static CurrentUserVO getCurrentUser() {
        return CURRENT_USER.get();
    }

    public static CurrentSystemVO getCurrentSystem(@NonNull String systemId) {
        return CURRENT_SYSTEMS.stream()
                .filter(cs-> StringUtils.equals(cs.getSystemId(),systemId))
                .findFirst()
                .orElse(null);
    }

    public static void disposeSystem(@NonNull String systemId){
        var currentSystem = getCurrentSystem(systemId);
        if(Objects.nonNull(currentSystem)){
            currentSystem.getEvaluationViewMaps().clear();
            CURRENT_SYSTEMS.remove(currentSystem);
        }
    }

    public static <T extends AbstractFxmlView> AbstractFxmlView getEvaluationView(@NonNull String systemId,Class<T> clazz){
        var currentSystem = CURRENT_SYSTEMS.stream()
                .filter(cs->StringUtils.equals(cs.getSystemId(),systemId))
                .findFirst().orElse(null);
        if(Objects.nonNull(currentSystem)){
            if(Objects.isNull(currentSystem.getEvaluationViewMaps())){
                currentSystem.setEvaluationViewMaps(new ConcurrentHashMap<>(30));
            }
            if(currentSystem.getEvaluationViewMaps().containsKey(clazz.getName())){
                return currentSystem.getEvaluationViewMaps().get(clazz.getName());
            }else{
                AbstractFxmlView view = SpringUtils.getBean(clazz);
                currentSystem.getEvaluationViewMaps().put(clazz.getName(),view);
                return view;
            }
        }else{
            return null;
        }
    }

    public static void addCurrentSystem(@NonNull CurrentSystemVO currentSystem) {
        boolean notExist = CURRENT_SYSTEMS.stream()
                .noneMatch(cs->StringUtils.equals(cs.getSystemId(),currentSystem.getSystemId()));
        if(notExist){
            CURRENT_SYSTEMS.add(currentSystem);
        }
    }

    public static void clearCurrentSystem(){
        CURRENT_SYSTEMS.clear();
    }

    public static Map<String, Class<? extends AbstractFxmlView>> getMenuViews() {
        return MENU_VIEWS;
    }

    public static Map<String, Class<?>> getMenuControllers() {
        return MENU_CONTROLLERS;
    }

    public  static void close(){
        CURRENT_SYSTEMS.stream()
                .filter(sys->Objects.nonNull(sys.getEvaluationStage()))
                .forEach(sys->{
                    sys.getEvaluationStage().close();
                    sys.setEvaluationScene(null);
                    sys.setEvaluationStage(null);
                });
    }
}